// import * as types from '../mutation-types'
import { invoke } from '@/api/base.js'
const listFilter = {
  state: {
    editData: {},
    conditions: [],
    orderableFields: [],
    orders: [],
    visibleFields: [],
    visibles: [],
    unitId: '',
    units: [],
    constant: {},
    selectItems: {}
  },

  getters: {
    editData: state => state.editData,
    getFilterConditions: state => state.conditions,
    // order
    getFilterOrderableFields: state => state.orderableFields,
    getFilterOrders: state => state.orders,
    // visiable
    getFilterVisibleFields: state => state.visibleFields,
    getFilterVisibles: state => state.visibles,
    // units
    getFilterUnitId: state => state.unitId,
    getFilterUnits: state => state.units,
    getFilterConstant: state => state.constant,
    getFilterSelectItems: state => state.selectItems
  },

  mutations: {
    SET_EDITDATA: (state, editData) => {
      state.editData = editData
    },
    SET_FILTER_CONDITIONS: (state, conditions) => {
      state.conditions = conditions
    },
    SET_FILTER_CONDITIONS_BY_INDEX: (state, {index, condition}) => {
      let temp = state.conditions
      temp[index] = condition
      state.conditions = temp
    },
    SET_FILTER_ORDERABLE_FIELDS: (state, orderableFields) => {
      state.orderableFields = orderableFields
    },
    SET_FILTER_ORDERS: (state, orders) => {
      state.orders = orders
    },
    SET_FILTER_ORDERS_BY_INDEX: (state, {index, order}) => {
      let temp = state.orders
      temp[index] = order
      state.orders = temp
    },
    REMOVE_FILTER_ORDER: (state, order) => {
      state.orders.splice(state.orders.infexOf(order), 1)
    },
    SET_FILTER_VISIBLE_FIELDS: (state, visibleFields) => {
      state.visibleFields = visibleFields
    },
    SET_FILTER_VISIBLES: (state, visibles) => {
      state.visibles = visibles
    },
    SET_FILTER_UNIT_ID: (state, unitId) => {
      state.unitId = unitId
    },
    SET_FILTER_UNITS: (state, units) => {
      state.units = units
    },
    SET_FILTER_CONSTANT: (state, constant) => {
      state.constant = constant
    },
    SET_FILTER_SELECT_ITEMS: (state, {selectId, selectItems}) => {
      state.selectItems[selectId] = selectItems
    }
  },

  actions: {
    setFilterEditData ({commit}, editData) {
      commit('SET_EDITDATA', editData)
    },
    setFilterConditions ({commit}, conditions) {
      commit('SET_FILTER_CONDITIONS', conditions)
    },
    setFilterConditionsByIndex ({commit}, params) {
      commit('SET_FILTER_CONDITIONS_BY_INDEX', params)
    },
    setFilterOrderableFields ({commit}, orderableFields) {
      commit('SET_FILTER_ORDERABLE_FIELDS', orderableFields)
    },
    setFilterOrders ({commit}, orders) {
      commit('SET_FILTER_ORDERS', orders)
    },
    setFilterOrdersByIndex ({commit}, params) {
      commit('SET_FILTER_ORDERS_BY_INDEX', params)
    },
    remoteFilterOrder ({commit}, order) {
      commit('REMOVE_FILTER_ORDER', order)
    },
    setFilterVisibleFields ({commit}, visibleFields) {
      commit('SET_FILTER_VISIBLE_FIELDS', visibleFields)
    },
    setFilterVisibles ({commit}, visibles) {
      commit('SET_FILTER_VISIBLES', visibles)
    },
    setFilterUnitId ({commit}, unitId) {
      commit('SET_FILTER_UNIT_ID', unitId)
    },
    setFilterUnits ({commit}, units) {
      commit('SET_FILTER_UNITS', units)
    },
    setFilterConstant ({commit}, constant) {
      commit('SET_FILTER_CONSTANT', constant)
    },
    setFilterSelectItems ({commit}, selectItems) {
      commit('SET_FILTER_SELECT_ITEMS', selectItems)
    },
    // load
    loadSchemaData ({commit, rootGetters}, args) {
      const arg = _.omitBy(args, _.isNull)
      const listTitleJson = rootGetters.getListClientData
      return new Promise((resolve, reject) => {
        invoke('Ecp.QuerySchema.getItemData', arg).then(res => {
          let data = res.data
          // commit('SET_EDITDATA', data.editData)
          commit('SET_FILTER_CONDITIONS', data.condition.conditions)
          commit('SET_FILTER_ORDERS', data.order)
          let visibles = data.visible ? data.visible.map(data => data.value)
            : listTitleJson.titleJson.fields.map(data => data.name)
          commit('SET_FILTER_VISIBLES', visibles)
          resolve(data.editData)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // save
    saveSchema ({ commit }, args) {
      return new Promise((resolve, reject) => {
        invoke('Ecp.QuerySchema.save', args).then((res) => {
          resolve(res.schemaId)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // delete
    deleteSchema ({commit}, schemaId) {
      return new Promise((resolve, reject) => {
        invoke('Ecp.QuerySchema.delete', {entityIds: [schemaId]}).then((res) => {
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

// // initial state
// const state = {
//   // listFilter: {
//   conditions: [],
//   orderableFields: [],
//   orders: [],
//   visibleFields: [],
//   visibles: [],
//   unitId: '',
//   units: [],
//   constant: {},
//   selectItems: {}
//   // }
// }
// // getters
// const getters = {
//   getFilterConditions: state => state.conditions,
//   // order
//   getFilterOrderableFields: state => state.orderableFields,
//   getFilterOrders: state => state.orders,
//   // visiable
//   getFilterVisibleFields: state => state.visibleFields,
//   getFilterVisibles: state => state.visibles,
//   // units
//   getFilterUnitId: state => state.unitId,
//   getFilterUnits: state => state.units,
//   getFilterConstant: state => state.constant,
//   getFilterSelectItems: state => state.selectItems
// }
//
// // actions
// const actions = {
//   setFilterConditions ({commit}, conditions) {
//     commit(types.SET_FILTER_CONDITIONS, conditions)
//   },
//   setFilterConditionsByIndex ({commit}, params) {
//     commit(types.SET_FILTER_CONDITIONS_BY_INDEX, params)
//   },
//   setFilterOrderableFields ({commit}, orderableFields) {
//     commit(types.SET_FILTER_ORDERABLE_FIELDS, orderableFields)
//   },
//   setFilterOrders ({commit}, orders) {
//     commit(types.SET_FILTER_ORDERS, orders)
//   },
//   setFilterOrdersByIndex ({commit}, params) {
//     commit(types.SET_FILTER_ORDERS_BY_INDEX, params)
//   },
//   setFilterVisibleFields ({commit}, visibleFields) {
//     commit(types.SET_FILTER_VISIBLE_FIELDS, visibleFields)
//   },
//   setFilterVisibles ({commit}, visibles) {
//     commit(types.SET_FILTER_VISIBLES, visibles)
//   },
//   setFilterUnitId ({commit}, unitId) {
//     commit(types.SET_FILTER_UNIT_ID, unitId)
//   },
//   setFilterUnits ({commit}, units) {
//     commit(types.SET_FILTER_UNITS, units)
//   },
//   setFilterConstant ({commit}, constant) {
//     commit(types.SET_FILTER_CONSTANT, constant)
//   },
//   setFilterSelectItems ({commit}, selectItems) {
//     commit(types.SET_FILTER_SELECT_ITEMS, selectItems)
//   }
// }
//
// // mutations
// const mutations = {
//   [types.SET_FILTER_CONDITIONS] (state, conditions) {
//     state.conditions = conditions
//   },
//   [types.SET_FILTER_CONDITIONS_BY_INDEX] (state, {index, condition}) {
//     let temp = state.conditions
//     temp[index] = condition
//     state.conditions = temp
//   },
//   [types.SET_FILTER_ORDERABLE_FIELDS] (state, orderableFields) {
//     state.orderableFields = orderableFields
//   },
//   [types.SET_FILTER_ORDERS] (state, orders) {
//     state.orders = orders
//   },
//   [types.SET_FILTER_ORDERS_BY_INDEX] (state, {index, order}) {
//     let temp = state.orders
//     temp[index] = order
//     state.orders = temp
//   },
//   [types.SET_FILTER_VISIBLE_FIELDS] (state, visibleFields) {
//     state.visibleFields = visibleFields
//   },
//   [types.SET_FILTER_VISIBLES] (state, visibles) {
//     state.visibles = visibles
//   },
//   [types.SET_FILTER_UNIT_ID] (state, unitId) {
//     state.unitId = unitId
//   },
//   [types.SET_FILTER_UNITS] (state, units) {
//     state.units = units
//   },
//   [types.SET_FILTER_CONSTANT] (state, constant) {
//     state.constant = constant
//   },
//   [types.SET_FILTER_SELECT_ITEMS] (state, {selectId, selectItems}) {
//     state.selectItems[selectId] = selectItems
//   }
// }

// export default {
//   state,
//   getters,
//   actions,
//   mutations
// }
export default listFilter
